<?php

namespace app\models;

use app\utils\BaseUtils;
use yii;
use app\components\BaseActiveRecord;

/**
 * This is the model class for table "{{%job}}".
 *
 * @property integer $id
 * @property integer $uid
 * @property integer $type
 * @property string $title
 * @property string $company
 * @property string $job_type
 * @property string $salary
 * @property string $city
 * @property string $address
 * @property string $work_time
 * @property string $experience1
 * @property string $experience2
 * @property string $experience3
 * @property string $experience4
 * @property string $experience5
 * @property string $experience6
 * @property string $thumbs
 * @property integer $status
 * @property integer $comments
 * @property integer $apply_number
 * @property integer $ctime
 *
 * @property User $user
 */
class Job extends BaseActiveRecord {

    const STATUS_VERIFY = 0;
    const STATUS_VERIFY_DENY = 1;
    const STATUS_SUCCESS = 2;
    const STATUS_DELETE = 3;

    // 1: 兼职 2: 实习
    const TYPE_JOB = 1;
    const TYPE_PRACTICE = 2;

    public static $typeLabels = [
        self::TYPE_JOB => '兼职',
        self::TYPE_PRACTICE => '实习',
    ];

    public static $cityLabels = [
        '北京' => '北京', '上海' => '上海'
    ];

    public static $workLabels = [
        '1天' => '1天', '2天' => '2天', '5天' => '5天', '7天' => '7天'
    ];

    public static $jobLabels = [
        '日结' => '日结', '周结' => '周结', '月结' => '月结'
    ];

    public static $statusLabels = [
        self::STATUS_VERIFY => '待审核',
        self::STATUS_VERIFY_DENY => '审核未通过',
        self::STATUS_SUCCESS => '审核通过',
        self::STATUS_DELETE => '已删除',
    ];

    public static $statusColors = [
        self::STATUS_VERIFY => 'warning',
        self::STATUS_VERIFY_DENY => 'danger',
        self::STATUS_SUCCESS => 'success',
        self::STATUS_DELETE => 'warning',
    ];

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%job}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['title', 'thumbs', 'salary', 'type', 'address'], 'required'],
            ['type', 'in', 'range' => [static::TYPE_JOB, static::TYPE_PRACTICE]],

            [['uid', 'type', 'status', 'comments', 'apply_number', 'ctime'], 'integer'],
            [['title', 'company', 'job_type', 'salary', 'address', 'work_time'], 'string', 'max' => 120],
            [['city'], 'string', 'max' => 60],
            [['experience1', 'experience2', 'experience3', 'experience4', 'thumbs'], 'string', 'max' => 300],
            [['experience5', 'experience6'], 'string', 'max' => 600]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'uid' => 'Uid',
            'type' => 'Type',
            'title' => '职位',
            'company' => '公司',
            'job_type' => '说明',
            'salary' => '薪资',
            'city' => '地区',
            'address' => '地址',
            'work_time' => 'Work Time',
            'experience1' => 'Experience1',
            'experience2' => 'Experience2',
            'experience3' => 'Experience3',
            'experience4' => '岗位职责',
            'experience5' => '任职要求',
            'experience6' => 'Experience6',
            'thumbs' => 'Thumbs',
            'status' => '状态',
            'apply_number' => '报名数',
            'ctime' => '发布时间',
        ];
    }

    public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'uid']);
    }

    public function beforeSave($insert) {
        if ($insert) {
            $this->status = static::STATUS_VERIFY;
        } else {
            if ($this->getIsVerifyDeny()) {
                $this->status = static::STATUS_VERIFY;
            }
        }
        return parent::beforeSave($insert);
    }

    public function getThumbs() {
        return yii\helpers\Json::decode($this->thumbs);
    }

    public function getThumb($default = '') {
        $thumbs = yii\helpers\Json::decode($this->thumbs);
        $thumb = empty($thumbs) ? $default : current($thumbs);
        return BaseUtils::formatImage($thumb);
    }

    public function getContentImage() {
        $thumbs = yii\helpers\Json::decode($this->thumbs);
        if (empty($thumbs) || count($thumbs) < 2) {
            return [];
        }
        array_shift($thumbs);
        return $thumbs;
    }

    public function getIsJob() {
        return intval($this->type) === intval(static::TYPE_JOB);
    }

    public function beforeValidate() {
        if (is_array($this->thumbs) && !empty($this->thumbs)) {
            $this->thumbs = yii\helpers\Json::encode($this->thumbs);
        }

        return parent::beforeValidate();
    }

    public function getIsApply($user) {
        if (!$user) {
            return FALSE;
        }
        /* @var $user User */
        if (!$user->getIsStudent()) {
            return TRUE;
        }

        if (ActivityApply::getIsApply($this->id, $user->id)) {
            return TRUE;
        }
        return FALSE;
    }

    public function getIsVerify() {
        return intval($this->status) === intval(self::STATUS_VERIFY);
    }

    public function getIsVerifyDeny() {
        return intval($this->status) === intval(self::STATUS_VERIFY_DENY);
    }
}
